<?php
$this->title = '个人信息变更';
?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>设置</title>
    <meta name="viewport" content="width=device-width,initial-scale=1.0,user-scalable=0" />
    <meta name="format-detection" content="telephone=no,email=no" />
    <meta name="mobileOptimized" content="width" />
    <meta name="handheldFriendly" content="true" />
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="apple-mobile-web-app-status-bar-style" content="black" />
    <link rel="stylesheet" href="/static/css/main.css" />
    <script src="/static/script/zepto.min.js"></script>
</head>
<body class="m-my-set u-bg1">
<div class="m-remark">
    <textarea placeholder="请输入不超过15个字符" rows="4" id="param"><?php
    $param = yii::$app->request->get();
    $key = '';
    foreach($param as $k => $item){
        echo $item;
        $key = $k;
        break;
    }
    ?></textarea>
    <a href="javascript:update('<?= $key?>');"><span>确定</span></a>
</div>

<script>
    function update(key){
//        console.log(key);
        var val = $('#param').val();
        if(key == 'mobile') {
            var reg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|145|147|170|176|177|178)+\d{8})$/;
            if (!reg.test(val)) {
                msgPopup("手机号码有误,请重新输入");
                $('#param').val(val);
                return false;
            }
        } else if(key == 'email') {
            var myreg = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
            if (!myreg.test(val)) {
                msgPopup("邮箱格式有误,请重新输入");
                $('#param').val(val);
                return false;
            }
        }else if(key == 'nickname'){
            val = $.trim(val);
            if(val.length<2){
                msgPopup("用户昵称最少包含2个字符");
                $('#param').val(val);
                return false;
            }
            if(val.length>15){
                msgPopup("昵称长度不可超过15个字符，请重新输入");
                $('#param').val(val);
                return false;
            }
        }
//        console.log(val);
        $.ajax({
            type: "get",
            dataType: "json",
            async: true,
            url: "/web/user/update?" + key + "=" + val,
            success: function (data) {
                window.location.href = '/web/user';
            },
            error: function (data) {
                console.log(data.response);
                msgPopup(data.response);
            }
        });
    }
</script>
</body>
</html>